package com.boco.pciv.service.company.impl;

import cn.hutool.core.util.StrUtil;
import com.boco.pciv.SysProperties;
import com.boco.pciv.core.service.PcivBaseServiceImpl;
import com.boco.pciv.entity.project.CompanyInfo;
import com.boco.pciv.entity.sys.UserInfo;
import com.boco.pciv.mapper.company.ICompanyMapper;
import com.boco.pciv.service.company.ICompanyService;
import com.boco.pciv.service.sys.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wzx on 2020/7/10.
 */
@Service
public class CompanyServiceImpl extends PcivBaseServiceImpl<ICompanyMapper,CompanyInfo,Integer> implements ICompanyService {
    Logger logger = LoggerFactory.getLogger(CompanyServiceImpl.class);
    @Autowired
    private IUserService iUserService;
    @Autowired
    SysProperties sysConfig;

    public CompanyInfo getCompanyInfo(String id){
        return mapper.getCompanyInfo(id);
    }

    public void addUser(CompanyInfo companyInfo) {
        UserInfo user = new UserInfo();
        user.setUserId(companyInfo.getPhone());
        user.setPhone(companyInfo.getPhone());
        user.setUserName(companyInfo.getUserName());
        user.setUserType(1);
        user.setDeptId(companyInfo.getLicNo());
        user.setPassword(companyInfo.getDefPwd());
        user.setCreateUser(companyInfo.getCreateUser());
        iUserService.addOther(user);
        
        //添加对应的角色权限
        Map<String,String> reMap = new HashMap<String, String>();
        reMap.put("userId",companyInfo.getPhone());
        reMap.put("roleId",companyInfo.getCompanyid());
        iUserService.insertUserRole(reMap);
    }
    public void deleteCompany(Map<String, Object> map){
        mapper.deleteCompany(map);
    }
    public void updateUserStatus(Map<String, Object> map){
        mapper.updateUserStatus(map);
    }
    public String reColoms(CompanyInfo info) {
        Map<String, Object> map = new HashMap();
        map.put("name", info.getName());
        map.put("licNo", info.getLicNo());
        map.put("phone", info.getPhone());
        List<CompanyInfo> list = mapper.queryReList(map);
        String str = "";
        for (CompanyInfo l : list) {
            if (l.getName().equals(info.getName())) {
                str += "系统存在重复的"+sysConfig.getCompanyName()+"名称；";
            }
            if (l.getLicNo().equals(info.getLicNo())) {
                str += "系统存在重复的"+sysConfig.getCompanyName()+"营业执照号；";
            }
            if (l.getPhone().equals(info.getPhone())) {
                str += "系统存在重复的"+sysConfig.getCompanyName()+"联系人手机号；";
            }
        }
        return str;
    }

    public List<CompanyInfo> queryList(Map<String, Object> map) {
        return mapper.queryList(map);
    }

    public List<CompanyInfo> queryReList(Map<String, Object> map) {
        return mapper.queryReList(map);
    }

    public String upload(List<Map<String, Object>> readAll, String type,String curUserId,String pwd) {
        String result = "";
        List<CompanyInfo> list = new ArrayList<>();
        for (int i = 0; i < readAll.size(); i++) {
            Map<String, Object> t = readAll.get(i);
            try {
                CompanyInfo info = getExcelInfo(t);
                if (StrUtil.isBlank(info.getName())) {
                    result += (i + 2) + "行 企业名称不能为空;";
                    return result;
                }
                if (StrUtil.isBlank(info.getLicNo())) {
                    result += (i + 2) + "行 营业执照号不能为空;";
                    return result;
                }
                if (StrUtil.isBlank(info.getUserName())) {
                    result += (i + 2) + "行 联系人不能为空;";
                    return result;
                }
                if (StrUtil.isBlank(info.getPhone())) {
                    result += (i + 2) + "行 手机号不能为空;";
                    return result;
                }
                if (StrUtil.isNotBlank(info.getPhone()) && info.getPhone().length() != 11) {
                    result += (i + 2) + "行 手机号格式不正确;";
                    return result;
                }
                String defpwd = pwd + info.getPhone().substring(7, 11);
                info.setCreateUser(curUserId);
                info.setDefPwd(defpwd);
                list.add(info);
            } catch (Exception ex) {
                result += (i + 1) + ";";
                logger.error("", ex);
            }
        }

        for (int i = 0; i < list.size(); i++) {
            CompanyInfo info1 = list.get(i);
            for (int j = 0; j < list.size(); j++) {
                if (i != j) {
                    CompanyInfo info2 = list.get(j);
                    if (info1.getName().equals(info2.getName())) {
                        result += (i + 2) + " " + (j + 2) + "行 企业名称 重复";
                        return result;
                    }
                    if (info1.getLicNo().equals(info2.getLicNo())) {
                        result += (i + 2) + " " + (j + 2) + "行 营业执照号 重复";
                        return result;
                    }
                    if (info1.getPhone().equals(info2.getPhone())) {
                        result += (i + 2) + " " + (j + 2) + "行 手机号 重复";
                        return result;
                    }
                }
            }
            if (type.equals("add")) {
                String str = reColoms(info1);
                if (StrUtil.isNotBlank(str)) {
                    return (i + 2) +"行"+str;
                }
            }
        }

        if (type.equals("all")) {
            List<CompanyInfo> ls = mapper.queryList(new HashMap());
            for (CompanyInfo l : ls) {
                iUserService.deleteByUserId(l.getPhone());
                mapper.delete(l.getId());
            }
        }
        for (CompanyInfo info : list) {
            addUser(info);
            mapper.insert(info);
        }

        return result;
    }

    private CompanyInfo getExcelInfo(Map<String, Object> map) {
        CompanyInfo info = new CompanyInfo();
        info.setName(map.get("企业名称").toString().trim());
        info.setLicNo(map.get("营业执照号").toString().trim());
        info.setUserName(map.get("联系人").toString().trim());
        info.setPhone(map.get("手机号").toString().trim());
        return info;
    }
}
